package com.ljt.common.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * html帮助类
 * @author chenting
 *
 */
@Slf4j
public class HttpUtils {

	/**
	 * css链接地址正则
	 */
	public static String cssRegex = "<link(\\W.*)(\\w.*)href=\"\\w.*?\"\\s*[/]{0,1}>";
	/**
	 * 截取HTML中的CSS样式
	 * @param html
	 * @return
	 */
	public static String getCssFile(String html) {
		html = "<link rel=\"stylesheet\" href=\"http://127.0.0.1:8080/wordsystem/resource/js/layer/skin/layer.css?v=1481185129776\">\r\n"+
			   "<link rel=\"stylesheet\" href=\"http://127.0.0.1:8080/wordsystem/resource/css/nopublic.css?v=1481185129776\" />\r\n"+
			   "<link rel=\"stylesheet\" href=\"http://127.0.0.1:8080/wordsystem/resource/css/editormd.preview.css?v=1481185129776\" />\r\n"+
			   "<link rel=\"stylesheet\" href=\"http://127.0.0.1:8080/wordsystem/resource/css/iconfont/iconfont.css?v=1481185129776\" />\r\n";
		Pattern r = Pattern.compile(cssRegex);
		Matcher m = r.matcher(html);
		StringBuffer cssStr = new StringBuffer();
		while (m.find()) {
			String temp = m.group();
			temp = temp.substring(temp.indexOf("href=\"")+6, temp.lastIndexOf("\""));
			log.info(temp);
			cssStr.append(sendPost(temp, "")).append("\r\n");
		}
		return cssStr.toString();
	}

	/**
	 * 根据正则表达式获取指定的字符串数组
	 * @param html
	 * @param regex
	 * @return
	 */
	public static String[] getHtmlStr(String html, String regex) {
		List<String> list = new ArrayList<String>();
		Pattern r = Pattern.compile(regex);
		Matcher m = r.matcher(html);
		if(m.find()) {
			list.add(m.group());
		}
		return list.toArray(new String[]{});
	}
	public static byte[] sendGet(String url){
        long st = System.currentTimeMillis();

        try {
            URL realURL = new URL(url);
            // 打开和URL之间的连接
            URLConnection connection = realURL.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立实际的连接
            connection.connect();
            BufferedImage read = ImageIO.read(connection.getInputStream());
            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
            //加载图片
            ImageIO.write(read, "jpg", byteArrayOut);
            log.info("请求耗时："+(System.currentTimeMillis()-st)+"毫秒");
            return byteArrayOut.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
	/**
     * 向指定URL发送GET方法的请求
     *
     * @param url
     *            发送请求的URL
     * @param param
     *            请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
     * @return URL 所代表远程资源的响应结果
     */
    public static String sendGet(String url, String param) {
    	long st = System.currentTimeMillis();
        String result = "";
        BufferedReader in = null;
        try {
        	String urlNameString = url;
        	if(StringUtils.isNotEmpty(param)) {
        		urlNameString += (url+"").indexOf("?") == -1 ? ("?" + param) : ("&" + param);
        	}
        	log.info(urlNameString);
            URL realURL = new URL(urlNameString);
            // 打开和URL之间的连接
            URLConnection connection = realURL.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
            	log.info(key + "--->" + map.get(key));
            }
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
        	log.error("发送GET请求出现异常！",e);
            e.printStackTrace();
            result = "error" ;
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    	log.info("请求耗时："+(System.currentTimeMillis()-st)+"毫秒");
        return result;
    }

    /**
     * 向指定 URL 发送POST方法的请求
     *
     * @param url
     *            发送请求的 URL
     * @param param
     *            请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
     * @return 所代表远程资源的响应结果
     */
    public static String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
//            System.out.println(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
        	log.error("发送 POST 请求出现异常！",e);
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }

    public static String sendPost(String url,Map<String,Object> paramMap,Map<String,String> customConfig) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
//            System.out.println(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("Accept-Charset", "UTF-8");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            if(null != customConfig && customConfig.size()> 0){
            	for(Entry<String, String> entry : customConfig.entrySet()){
            		conn.addRequestProperty(entry.getKey(), entry.getValue());
            	}
            }
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "utf-8"));
            log.info("请求地址:"+url);
            if(paramMap != null && paramMap.size() > 0) {
            	String paramJson = JSONObject.toJSONString(paramMap);
            	log.info("请求参数:"+paramJson);
                // 发送请求参数
                out.print(paramJson);
            }
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
        	log.error("发送 POST 请求出现异常！",e);
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }


    /**
     * 向指定 URL 发送POST方法的请求
     *
     * @param url
     *            发送请求的 URL
     * @param param
     *            请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
     * @return 所代表远程资源的响应结果
     */
    public static String sendPost(String url ,Map<String,Object> paramMap) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        String param = "";
        try {
            URL realUrl = new URL(url);
//            System.out.println(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // conn.setConnectTimeout(5000);
            conn.setReadTimeout(180000);
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());

            //拼接参数
            Set<Entry<String,Object>> set = paramMap.entrySet();
            for (Entry<String, Object> entry : set) {
            	if("null".equals(entry.getValue())){
            		param += "&" + entry.getKey()+"="+ "";
            	}else {
            		param += "&" + entry.getKey()+"="+ URLEncoder.encode(String.valueOf( entry.getValue()),"UTF-8");
				}

			}
            if(param != null && !"".equals(param) ){
            	param = param.substring(1);
            }
            log.info("请求地址:"+url);
            log.info("请求参数:"+param);
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
        	log.error("发送 POST 请求出现异常！",e);
            e.printStackTrace();
            result = "error" ;
        }
        //使用finally块来关闭输出流、输入流
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }


	public static void main(String[] args) {
		String url = "http://192.168.10.199:9002/gisPlatform/user/login.do";
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("userAccount", "admin");
		paramMap.put("password", "e10adc3949ba59abbe56e057f20f883e");
		String result = sendPost(url, paramMap);
		System.out.println(result);//error
	}




}
